package com.netease.streamlearningbyjava;

import com.netease.streamlearningbyjava.bean.Employee2;

import org.junit.Test;

import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 创建日期：2021/11/8 10:58
 *
 * @author tony.sun
 * 类说明：规约与收集
 */

public class Stream12SpecificationsAndCollect {
    List<Employee2> employee2s = Arrays.asList(
            new Employee2("张三", 18, 9999.99, Employee2.Status.FREE),
            new Employee2("李四", 58, 5555.55, Employee2.Status.BUSY),
            new Employee2("王五", 26, 3333.33, Employee2.Status.VOCATION),
            new Employee2("赵六", 36, 6666.66, Employee2.Status.FREE),
            new Employee2("赵六", 36, 6666.66, Employee2.Status.FREE),
            new Employee2("田七", 12, 8888.88, Employee2.Status.BUSY)
    );
    /**
     * 规约
     * reduce（T identity，BinaryOperator）/reduce(BinaryOperator)--可以将流中的元素反复结合起来，得到一个值。
     */
    @Test
    public void test3(){
        List<Integer> list= Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        Integer sum = list.stream()
                .reduce(0, (x, y) -> x + y);//这个0是起始值，第一个值，y是上面的1,2,3,4,5,6,7,8,9,10，每次都加一次，加了10次
        System.out.println(sum);//55

        System.out.println("----------------------------------");
        Optional<Double> optional = employee2s.stream()
                .map(Employee2::getSalary)
                .reduce(Double::sum);
        System.out.println(optional);
    }
    /**
     * 收集
     * collect--将流转化为其他形式，接收一个Collector接口实现，用于给Stream中元素做汇总的方法
     */
    @Test
    public void test4(){
        //提取老的list里面的值，变成一个新的list（只有name），然后打印
        List<String> list = employee2s.stream()
                .map(Employee2::getName)
                .collect(Collectors.toList());
        list.forEach(System.out::println);
        //结果：
        //张三
        //李四
        //王五
        //赵六
        //赵六
        //田七
        //去重
        System.out.println("--------------------------------");
        Set<String> set = employee2s.stream()
                .map(Employee2::getName)
                .collect(Collectors.toSet());
        set.forEach(System.out::println);
        //结果：
        //李四
        //张三
        //王五
        //赵六
        //田七
        /**
         * 其他格式
         */
        HashSet<String> hashSet = employee2s.stream()
                .map(Employee2::getName)
                .collect(Collectors.toCollection(HashSet::new));
        hashSet.forEach(System.out::println);


    }

    @Test
    public void test5(){
        /**
         * 总数
         */
        Long count = employee2s.stream()
                .collect(Collectors.counting());
        System.out.println(count);
        /**
         * 平均值
         */
        Double avg = employee2s.stream()
                .collect(Collectors.averagingDouble(Employee2::getSalary));
        System.out.println(avg);
        /**
         * 总和
         */
        DoubleSummaryStatistics sum = employee2s.stream()
                .collect(Collectors.summarizingDouble(Employee2::getSalary));
        System.out.println(sum);
        /**
         * 最大值
         */
        Optional<Employee2> max = employee2s.stream()
                .collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())));
        System.out.println(max);
        /**
         * 最小值
         */
        Optional<Double> min = employee2s.stream()
                .map(Employee2::getSalary)
                .collect(Collectors.minBy(Double::compare));
        System.out.println(min.get());
        /**
         * 分组
         */
        Map<Employee2.Status, List<Employee2>> group = employee2s.stream()
                .collect(Collectors.groupingBy(Employee2::getStatus));
        System.out.println(group);
    }

}
